package com.dt.controller;

import com.dt.common.exception.ExceptionPrintUtil;
import com.dt.common.response.ResponseCodeEnum;
import com.dt.common.response.ResponseData;
import com.dt.entity.Department;
import com.dt.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门相关接口
 *
 * @author JOSWAY
 */
@Api(tags = "部门相关接口")
@CrossOrigin
@RestController
@RequestMapping("/department")
public class DepartmentController {
    private final static Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);
    @Resource
    DepartmentService departmentService;

    @ApiIgnore
    @ApiOperation(value = "删除部门信息")
    @DeleteMapping(value = "delete-department")
    public ResponseData<Integer> deleteDepartment(
            @ApiParam(value = "部门主键", required = true)
                    Integer departmentId) {
        try {
            return new ResponseData<Integer>(ResponseCodeEnum.SUCCESS, "成功", departmentService.deleteByPrimaryKey(departmentId));
        } catch (Exception e) {
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "添加部门信息")
    @PostMapping(value = "insert-department")
    public ResponseData<Integer> insertDepartment(
            @ApiParam(value = "部门编号", required = true)
                    Integer departmentId,
            @ApiParam(value = "部门名称", required = true)
                    String departmentName) {

        try {
            Department department = new Department();
            department.setDepartmentId(departmentId);
            department.setDepartmentName(departmentName);

            return new ResponseData<Integer>(ResponseCodeEnum.SUCCESS, "成功", departmentService.insertSelective(department));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "查询所有部门")
    @Deprecated
    @GetMapping(value = "select-all-department")
    public ResponseData<List<Department>> selectAllDepartment() {
        try {
            return new ResponseData<List<Department>>(ResponseCodeEnum.SUCCESS, "成功", departmentService.selectAll());
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "查询部门信息")
    @GetMapping(value = "select-one-department")
    public ResponseData<Department> selectOneDepartment(
            @ApiParam(value = "部门主键", required = true)
                    Integer departmentId) {
        try {
            return new ResponseData<Department>(ResponseCodeEnum.SUCCESS, "成功", departmentService.selectByPrimaryKey(departmentId));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "修改部门信息")
    @PutMapping(value = "update-department")
    public ResponseData<Integer> updateDepartment(
            Department department) {

        try {
            return new ResponseData<Integer>(ResponseCodeEnum.SUCCESS, "成功", departmentService.updateByPrimaryKeySelective(department));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }
}